//
// Created by tang on 23-3-11.
//

#include "HefengWeatherPlugin.hpp"
#include <service/timer_service.hpp>
#include "content/image.hpp"
#include "content/view_base.hpp"
#include "core/assert.hpp"
#include "core/log.hpp"
#include "core/timespan.hpp"
#include "icon_manager.hpp"
#include "plugin/plugin_impl.hpp"

static IconManager iconManager;

static pi_time_t update_intervals[] {
    15*60.0,
    30*60.0,
    60*60.0,
    3*60*60.0,
    6*60*60.0,
    12*60*60.0,
};

HefengWeatherPlugin::HefengWeatherPlugin () : httpService_ {nullptr}, cfgService_ {nullptr} {}
HefengWeatherPlugin::~HefengWeatherPlugin () {}

const PluginInfo *HefengWeatherPlugin::get_plugin_info () const
{
    return &kPluginInfo;
}

bool HefengWeatherPlugin::initialize ()
{
#if defined(__GNUC__) || defined (__clang__)
    L_DEBUG (__PRETTY_FUNCTION__);
#elif defined(_MSC_VER)
    L_DEBUG (__FUNCSIG__);
#endif

    httpService_ = ServiceManager::instance ()->request_service<IHttpService> ();
    cfgService_  = ServiceManager::instance ()->request_service<IConfigService> ();

    auto cfg_section = cfgService_->group ("hefeng_weather", name ());
    cfg_section->secret ("api_key", "API密钥", api_key_, "None", "调用接口使用的API_KEY");
    cfg_section->select (
            "update_interval", "更新时间周期", update_interval_,
            [] (void *, int idx) -> const char * {
                static const char *intervals[] = {
                        "每15分钟", "每30分钟", "每1小时", "每3小时", "每6小时", "每12小时",
                };
                PI_ASSERT (idx < (int) std::size (intervals));
                return intervals[idx];
            },
            6, nullptr, 0, "选择数据更新的时间周期");
    cfg_section->data ("city_id", city_id_, "");

    iconManager.load_icons ();

    RESOURCE_IMAGE (icon_, "/assets/img/qweather.png");

    L_INFO ("API Key: %s", api_key_->read ().c_str ());
    L_INFO ("City ID is: %s", city_id_->c_str ());

    RESOURCE_VIEW (main_view_, "/layouts/main.xml");
    data_ = new HefengData {api_key_->read (), city_id_, &iconManager};
    main_view_->bind (data_->get_bindable());

    return true;
}

void HefengWeatherPlugin::tick (pi_time_t delta, pi_time_t total) {

    bool interval_changed = update_interval_->changed();
    if (interval_changed) {
        update_interval_->handled();
        data_->do_update_weather();
        last_update_ = total + 1;
    } else if (last_update_ == 0 || total - last_update_ > update_intervals[update_interval_->read()]) {
        data_->do_update_weather();
        last_update_ = total + 1;
    }
}

void HefengWeatherPlugin::cleanup ()
{

    delete data_;
    delete main_view_;
    delete icon_;

    iconManager.free_icons ();

#if defined(__GNUC__) || defined (__clang__)
    L_DEBUG (__PRETTY_FUNCTION__);
#elif defined(_MSC_VER)
    L_DEBUG (__FUNCSIG__);
#endif
}
const char* HefengWeatherPlugin::name () const
{
    return "和风天气 \U0001F525";
}
const ImageContent *HefengWeatherPlugin::icon () const
{
    return icon_;
}
ui_view *HefengWeatherPlugin::content () const
{
    return main_view_;
}
